#include "User.h"
#include "bmp.h"
UI ui;


u8 wea_ui_flg = 1;//刷新数据标志
u8 high_freq_flg = 0;


void UI::refresh()
{
    oled_flg = 0;

    key_set();//按键事件处理

    sys_work_set();//检测系统休眠条件

    if(sys_wake_flg)
    { 
        if(menu_page_return_flg)
        {
            menu_page_return_flg = false;
            show_future_weather();
        }

        if(led_color_set_flg)
            led_color_set();
        
        switch (menu_page)
        {
            case 0:
            {
                love_show();
                data_updata();
                show_icon();
                break;
            }
            
            case 10:
            {
                future_text_move();
                break;
            }    
        }
        
    }
        
    




}








void UI::clr()
{
    oled.clr();
    oled.refresh();
}


void UI::key_set()//按键事件处理
{
    u8 key_buf= key.get_once();

    if(key_buf!=no_putdown)
    {
        night_dis_t = 0;
        if(sys_wake_flg)
        {   
            if(page_switch(key_buf))
                return;
            if(menu_page <2) //界面切换
            {
                if (key_buf == UP||key_buf == DOWN)
                {
                    if(menu_page == 0)
                    {
                        if(key_buf == UP)
                            wea_updata_flg = 1;
                        else
                            led_color_set_flg = 1;
                    }   
                    else if(menu_page == 1)
                        menu_choose(menu_roll,key_buf);//菜单界面滚动识别
                }
            }
            else if(menu_page == 2)//二级菜单界面设置
            {
                circle_move(0,key_buf);
                if(key_buf == RIGHT)
                {
                    if(set_menu_num == 0)//选项在第一项
                    {
                        if(menu_num == 0)
                        {
                            led.color_choose_mode = !led.color_choose_mode;
                            led_color_set_flg = 1;
                            turn_switch(102,3,led.color_choose_mode);
                            eeprom.write(led_color_mode_add,led.color_choose_mode);
                        }
                            // shutdown_flg = 1;
                        else if(menu_num == 1)
                        {
                            sleep_flg = !sleep_flg;
                            turn_switch(102,3,sleep_flg);
                            eeprom.write(low_power_flg_add,sleep_flg);
                            dat.low_power_set();
                        }
                        else if(menu_num == 2)
                        {
                            night_shutdown_flg = !night_shutdown_flg;
                            turn_switch(102,3,night_shutdown_flg);
                            eeprom.write(night_shudown_add,night_shutdown_flg);
                        }
                    }
                    if(menu_num == 3)
                    {

                        if(set_menu_num!=choose_menu_num)
                        {
                            if(set_menu_num<choose_menu_num)
                                circle_move(1,UP);
                            else
                                circle_move(1,DOWN);
                            location_mode = choose_menu_num;
                            eeprom.write(location_add,location_mode);
                            if(location_mode)
                                eeprom.read_all_data();
                            else
                                location_text = "";
                            hweather.locationID = "0";
                            wea_updata_flg = 1;
                        }
                    }
                }
            }
            else//三级菜单
            {
                if(menu_num == 0)//LED COLOR
                {
                    circle_move(0,key_buf);


                    if(set_dat_num == 1)
                    {
                        if(key_buf == RIGHT||key_buf == LEFT)
                            color_mix_circle_move(key_buf);

                    }
                    else
                    {
                        if(key_buf == RIGHT)
                        {
                            led_color_ch_move();
                            
                        }
                    }  
                }
                else if(menu_num == 2)//night shutdown
                {
                    circle_move(0,key_buf);
                    if(key_buf == RIGHT)
                    {
                        sleep_wake_hour_move(); 
                    }

                }
            }
        }
        else
        {
            wake();     //如系统处于睡眠状态则唤醒系统
        }
    }

}






bool UI::page_switch(u8 mode)
{
    short x = 128;
    short buf;
    short buf1;


    if(mode != LEFT&&mode != RIGHT)
        return 0;
    

    if(mode == RIGHT)
    {
        if(menu_page == 3)
            return 0;

        if(menu_page == 2)
        {
            if(set_menu_num != 1)
                return 0;
            if(menu_num == 3)
            {
                if(choose_menu_num != 1)
                    return 0;
                menu_page ++;
                location_ap_get();
                return 0; 
            }
            else if(menu_num == 1)
            {
                menu_page ++;
                set_cpu_freq(HIGH_FREQ);
                oled.roll(0,0,128,8,8,LEFT,16);
                setwifi.smart_conect(config_network);
                page_switch(LEFT);
                return 0;
            }
        }
            
        if(menu_page == 0)//桌面进行判断
        {
            show_icon_out();
            data_refresh_move_out();
            menu_choose(menu_turn,DOWN);
            button(IN);
            menu_page ++;
            return 1;
        }
        if(menu_page == 1)
            button(OUT);
        menu_page ++;
        if(menu_page != 3)
        {
            set_menu_num = 0;
            choose_menu_num = 3;
        }
        else
        {
            set_dat_num = 0;
        }
            
        
    }
    else
    {
        if(menu_page == 0 || menu_page == 10)
        {
            show_future_weather();
            return 0;
        }
        if(menu_page == 3&&menu_num == 0&&set_dat_num == 1)
            return 0;
        if(menu_page == 1)//由菜单界面返回桌面
        {
            button(OUT);
            set_cpu_freq(HIGH);
            oled.roll(32,0,64,8,4,UP,16);
            if(wea_updata_flg)
            {
                wea_updata_flg = 0;
                dat.refre_weather(0);
            }
            else
            {
                data_refresh_move_in();
                show_icon_in();
            }
            menu_page --;
            return 1;
        }
        menu_page --;
        x = 0;
    }

    oled.xy_set(0,0,128,8);
    set_cpu_freq(HIGH_FREQ);

        
    for(;;)
    {
        oled.clr();
        if(menu_page == 1||(mode == RIGHT&&menu_page == 2))
            oled.BMP(x - 96,0,64,64,get_munu_icon(menu_num),1);

        if(menu_page == 3 || (menu_page == 2&&mode == LEFT))
        {
            buf = x - 96;
            buf1 = x + 32;
        }
        else
        {
            buf = x + 32;
            if(menu_num == 4)
            buf += 32;
        }

        switch (menu_num)
        {
            case 0:
            {
                
                oled.chinese(buf,0,"随机颜色",16,1,0);
                oled.chinese(buf,24,"颜色调整",16,1,0);
                oled.BMP(buf - 15,3+set_menu_num *24,circle_kong);
                oled.BMP(buf + 70,27,jt_right);
                show_switch(buf + 70,3,led.color_choose_mode*9);
                break;
            }
            case 1:
            {
                oled.chinese(buf,0,"省电模式",16,1,0);
                oled.chinese(buf,24,"配置网络",16,1,0);
                oled.BMP(buf - 15,3+set_menu_num *24,circle_kong);
                oled.BMP(buf + 70,27,jt_right);
                show_switch(buf + 70,3,sleep_flg*9);
                oled.str(buf,45,wifima.ee_ssid,16,1,0);
                break;
            }
            case 2:
            {

                oled.chinese(buf,0,"定时关闭",16,1,0);
                oled.chinese(buf,24,"时间设置",16,1,0);
                oled.BMP(buf - 15,3+set_menu_num *24,circle_kong);
                oled.BMP(buf + 70,27,jt_right);
                show_switch(buf + 70,3,night_shutdown_flg*9);
                break;
            }
            
            case 3:
            {
                oled.chinese(buf,0,"网络位置",16,1,0);
                oled.chinese(buf,24,"精确位置",16,1,0);
                oled.BMP(buf - 15,3+location_mode*24,circle_shi);
                set_menu_num = location_mode;
                choose_menu_num = location_mode;
                oled.BMP(buf + 70,27,jt_right);
                if(hweather.location != "0")
                    oled.str(buf,45,hweather.location,16,1,0);
                break;
            }
            case 4:
            {
                oled.BMP(buf-64,0,64,64,QFTEK,1);
                oled.chinese(buf,0,"启凡科创",16,1,0);
                oled.chinese(buf,18,"智能天气",16,1,0);
                oled.chinese(buf,36,"时钟",16,1,0);
                oled.str(buf + 8,50,"Ver:1.0",16,1,0);
                break;
            }
        }

        if((mode == LEFT&&menu_page == 2)||menu_page == 3)
        {
            switch (menu_num)
            {
                case 0:
                {
                    led_color_ch(buf1,led.color_mode_buf);
                    oled.chinese(buf1,24,"蓝",16,1,0);
                    oled.chinese(buf1+48,24,"紫",16,1,0);
                    oled.BMP(buf1 - 15,3 + set_dat_num*24,circle_kong);
                    oled.BMP(buf1 + 70,3,jt_right);
                    color_mix_circle(buf1);
                    break;
                }
                case 2:
                {
                    oled.chinese(buf1 + 16,0,"时关闭",16,1,0);
                    oled.chinese(buf1 + 16,24,"时开启",16,1,0);
                    oled.BMP(buf1 - 15,3,circle_kong);
                    oled.BMP(buf1 + 70,3,jt_right);
                    oled.BMP(buf1 + 70,27,jt_right);
                    oled.num(buf1+16,0,sleep_hour,2,16,RIGHT,1);
                    oled.num(buf1+16,24,wake_hour,2,16,RIGHT,1);
                    oled.BMP(buf1 - 15,3 + set_dat_num*24,circle_kong);
                    break;
                }
            }

        }


        oled.refresh();
        yield();
        if(mode == LEFT)
            x += 8;
        else
            x -= 8;
        if(x == -8 || x == 136)
            break;
    }
    set_cpu_freq(LOW_FREQ);
    if(menu_page == 1)
        button(IN);
    return 1;
}




void UI::menu_choose(u8 able,u8 mode)//菜单应用图标滚动函数
{
    short y = 0;
    s8 yy;
    s8 buf;
    if(mode!= UP&&mode!=DOWN)
        return;

    oled.xy_set(0,0,128,8);
    set_cpu_freq(HIGH_FREQ);

    if(!able)
    {
        if(mode == UP)
            y = -64;
        else
            y = 64;
        for(;;)
        {
            oled.choose_clr(32,0,64,8);
            oled.BMP(32,y,64,64,get_munu_icon(menu_num),1);
            oled.choose_refresh(32,0,64,8);
            yield();
            switch (mode)
            {
                case UP:   y += 4;break;
                case DOWN: y -= 4;break;
            }
            if(y == 4 && mode == UP)
                break;
            if(y == -4 && mode ==  DOWN)
                break;
        }

    }
    else
    {
        if(mode == UP)
        {
            buf = menu_num - 1;
            if(buf < 0)
                buf = menu_num_max - 1;
        }
        else
        {
            buf = menu_num + 1;
            if(buf == menu_num_max)
                buf = 0;
        }

        if(mode == UP)
            yy = -64;
        else
            yy = 64;
        
        

        for(;;)
        {
            oled.choose_clr(32,0,64,8);
            
            oled.BMP(32,y,64,64,get_munu_icon(menu_num),1);
            oled.BMP(32,yy,64,64,get_munu_icon(buf),1);
            oled.choose_refresh(32,0,64,8);
            yield();
            switch (mode)
            {
                case UP:   y += 2;yy += 2;break;
                case DOWN: y -= 2;yy -= 2;break;
            }
            if(yy == 2 && mode == UP)
                break;
            if(yy == -2 && mode == DOWN)
                break;
        }
        menu_num = buf;
    }
    set_cpu_freq(LOW_FREQ);
        
}

const char *UI::get_munu_icon(u8 num)
{
    switch (num)
    {
        case 0: return led_color; break;
        case 1: return wlan; break;
        case 2: return day_nignt; break;
        case 3: return location; break;
        case 4: return about; break;
        default:return led_color; break;
    }
}

void UI::led_color_set()
{
    u8 buf;
    
    if(led.color_choose_mode)
    {
        for(;;)
        {
            buf = rand()%13;
            if(buf < 2)
            {
                for(;;)
                {
                    if(buf != led.color_mode)
                    {
                        led.color_mode = buf;
                        break;
                    }
                    yield();
                    buf = rand()%2;
                } 
                break; 
            }
            else
            {
                buf -= 2;
                led.color_mode = 2;
                for(;;)
                {
                    
                    if(buf != led.color_mix)
                    {
                        led.color_mix = buf;
                        break;
                    }
                    buf = rand()%11;
                    yield();
                }
                break;
            }
            yield();
        }
        
    }
    else
    {
      led.color_mode=led.color_mode_buf; 
      led.color_mix = led.color_mix_buf;
    }
    led_color_set_flg = 0;
    adc.last_dat = 0;
}



void UI::sys_work_set()
{
    if(night_shutdown_flg)
    {
        if(shutdown_flg)
        {
            shutdown();
        }
        if(pcf.hour ==sleep_hour&&sys_wake_flg&&night_dis_t == 11)
        {
            shutdown();
        }
        if(pcf.hour ==wake_hour&&!sys_wake_flg)
        {
            wake();
        }
    }
    if(pcf.hour == 4&&time_updata_flg)
    {
      time_updata_flg = 0;
      dat.refre_time();
    }
    if(pcf.hour == 2&&!time_updata_flg)
    {
      time_updata_flg = 1;
    }
}



void UI::data_updata()
{
    if(wea_updata_flg)
    {
        wea_updata_flg = 0;
        dat.refre_weather(1);
    }
    
}




void UI::shutdown()
{
    ticker40ms.detach();
    ticker3s.detach();
    ticker1m.detach();

    led.power(OFF);
    clr();
    
    oled.display_off();
    low_power_flg = 1;
    dat.low_power_set();
    
    led.end();
    shutdown_flg = 0;
    sys_wake_flg = 0;
    menu_page = 0;
}

void UI::wake()
{
    led.last_time_data[0] = 255;
    led.last_time_data[1] = 255;
    led.last_time_data[2] = 255;
    led.last_time_data[3] = 255;

    night_dis_t = 0;

    Ticker_init();

    led.begin();

    led.power(ON);
    oled.display_on();

    dat.refre_weather(0);
    wea_updata_flg = 0;
    sys_wake_flg = 1;
}




void UI::show_love()
{
    u8 count = 0;
    u8 x;
    show_icon_out();
    data_refresh_move_out();
    set_cpu_freq(HIGH_FREQ);
    oled.xy_set(0,0,128,8);
    while(1)
    {
        x =rand()%114;
        if(rand()%2)
            x =rand()%114;
        oled.BMP(x,rand()%50,15,15,kong_love,1);
        oled.choose_refresh(x,0,15,8);
        count++;
        if(count ==100)
            break;
        delay(100);
    }
    for(x=0;x<32;x++)
    {
        oled.roll(0,0,128,4,1,UP,1);
        oled.roll(0,4,128,4,1,DOWN,1);
    }
    data_refresh_move_in();
    show_icon_in(); 

}





void UI::icon_set(u16 icon)
{
    /*
    8少云 4多云 2月亮 1太阳
    8大雨 4中雨 2小雨 1毛毛雨
    8中雪 4小雪 2雷电 1暴雨
                2霾  1雾
    */

    switch (icon)
    {
        case 100:icon_flg = 0x0001;break;//白天晴
        case 101:icon_flg = 0x0004;break;//多云
        case 102:icon_flg = 0x0008;break;//少云
        case 103:icon_flg = 0x0005;break;//晴间多云
        case 104:icon_flg = 0x0009;break;//阴
        case 150:icon_flg = 0x0002;break;//夜晚晴
        case 151:icon_flg = 0x0004;break;//夜晚多云
        case 152:icon_flg = 0x0008;break;//夜晚少云
        case 153:icon_flg = 0x0006;break;//晴间多云
        case 154:icon_flg = 0x000a;break;//阴
        case 300:icon_flg = 0x0029;break;//阵雨
        case 301:icon_flg = 0x0089;break;//强阵雨
        case 302:icon_flg = 0x0249;break;
        case 303:icon_flg = 0x0289;break;
        case 304:icon_flg = 0x0249;hweather.weather = "雷雨冰雹";break;
        case 305:icon_flg = 0x0028;break;
        case 306:icon_flg = 0x0048;break;
        case 307:icon_flg = 0x0088;break;
        case 308:icon_flg = 0x0118;break;
        case 309:icon_flg = 0x0018;hweather.weather = "毛毛雨";break;
        case 310:icon_flg = 0x0108;break;
        case 311:icon_flg = 0x0108;break;
        case 312:icon_flg = 0x0108;break;
        case 313:icon_flg = 0x0088;break;
        case 314:icon_flg = 0x0028;break;
        case 315:icon_flg = 0x0048;break;
        case 316:icon_flg = 0x0088;break;
        case 317:icon_flg = 0x0108;hweather.weather = "大暴雨";break;
        case 318:icon_flg = 0x0118;hweather.weather = "特大暴雨";break;
        case 350:icon_flg = 0x002a;break;
        case 351:icon_flg = 0x008a;break;
        case 399:icon_flg = 0x0048;break;
        case 400:icon_flg = 0x0408;break;
        case 401:icon_flg = 0x0808;break;
        case 402:icon_flg = 0x0808;break;
        case 403:icon_flg = 0x0808;break;
        case 404:icon_flg = 0x0888;break;
        case 405:icon_flg = 0x0888;break;
        case 406:icon_flg = 0x0889;break;
        case 407:icon_flg = 0x0809;break;
        case 408:icon_flg = 0x0408;break;
        case 409:icon_flg = 0x0808;break;
        case 410:icon_flg = 0x0808;break;
        case 456:icon_flg = 0x084a;break;
        case 457:icon_flg = 0x080a;break;
        case 499:icon_flg = 0x0408;break;
        case 500:icon_flg = 0x1000;break;
        case 501:icon_flg = 0x1000;break;
        case 502:icon_flg = 0x2000;break;
        case 509:icon_flg = 0x1000;break;
        case 510:icon_flg = 0x1000;break;
        case 511:icon_flg = 0x2000;break;
        case 512:icon_flg = 0x2000;break;
        case 513:icon_flg = 0x2000;break;
        case 514:icon_flg = 0x1000;break;
        case 515:icon_flg = 0x1000;break;
        default:icon_flg = 0;break;
    }
}

void UI::show_icon()
{
    /*
    8少云 4多云 2月亮 1太阳
    8大雨 4中雨 2小雨 1毛毛雨
    8中雪 4小雪 2雷电 1暴雨
                2霾  1雾
    */
    oled.xy_set(0,0,64,8);
    if(icon_flg)
    {
        oled.choose_clr(0,0,64,8);
        if(icon_flg&0x0001)
            oled.BMP(son_moon_x,0,wea_son);
        if(icon_flg&0x0002)
            oled.BMP(son_moon_x,0,wea_moon);
        if(icon_flg&0x0004)
            oled.BMP(wea_x,16,wea_cloudy);
        if(icon_flg&0x0008)
            oled.BMP(wea_x,16,wea_less_cloudy);
        if(icon_flg&0x0010)
            oled.BMP(wea_x+4,wea_y,wea_drizzle);
        if(icon_flg&0x0020)
            oled.BMP(wea_x+4,wea_y,wea_small_rain);
        if(icon_flg&0x0040)
            oled.BMP(wea_x+4,wea_y,wea_middle_rain);
        if(icon_flg&0x0080)
            oled.BMP(wea_x+4,wea_y,wea_large_rain);
        if(icon_flg&0x0100)
            oled.BMP(wea_x+4,wea_y,wea_rainstorm);
        if(icon_flg&0x0200)
            oled.BMP(wea_x+20,wea_y+1,wea_thunder);
        if(icon_flg&0x0400)
            oled.BMP(wea_x+4,wea_y,wea_small_snow);
        if(icon_flg&0x0800)
            oled.BMP(wea_x+4,wea_y,wea_middle_snow);
        if(icon_flg&0x1000)
            oled.BMP(13,23,wea_fog);
        if(icon_flg&0x2000)
            oled.BMP(9,22,wea_smog);
        
        wea_t++;
        if(wea_t==10)
        {
            wea_t= 0;
            if(wea_mode)
            wea_x++;
            else
            wea_x--;

            if(wea_x==-20||wea_x==36)
            wea_mode = !wea_mode;

        }
        wea_y++;
        if(wea_y==64)
            wea_y=38;
        oled.choose_refresh(0,0,64,8);
    }     
}


void UI::show_icon_in()
{
    /*
    8少云 4多云 2月亮 1太阳
    8大雨 4中雨 2小雨 1毛毛雨
    8中雪 4小雪 2雷电 1暴雨
                2霾  1雾
    */
    short x;

    u8 son_moon = 2;
    set_cpu_freq(HIGH_FREQ);
    oled.xy_set(0,0,64,8);

    if(!icon_flg)
    {
        for(x=64;x>=0;x-=4)
        {
            oled.choose_clr(0,0,64,8);
            oled.BMP(0,x,bmp_cat);
            oled.choose_refresh(0,0,64,8);
            yield();
        }
    }
    else
    {
        if(icon_flg&0x0001)
        {
            son_moon = 1;
            for(x=64;x>=0;x-=2)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(son_moon_x,x,wea_son);
                oled.choose_refresh(0,0,64,8);
                yield();
            }
        }
            
        if(icon_flg&0x0002)
        {
            son_moon = 0;
            for(x=64;x>=0;x-=2)
            {
                oled.choose_clr(0,0,39,8);
                oled.BMP(son_moon_x,x,wea_moon);
                oled.choose_refresh(0,0,39,8);
                yield();
            }
                
        }
        if(icon_flg&0x0004)
        {
            for(x=-44;x<17;x+=2)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(wea_x,x,wea_cloudy);
                if(son_moon ==1)
                    oled.BMP(son_moon_x,0,wea_son);
                else if(!son_moon)
                    oled.BMP(son_moon_x,0,wea_moon);
                oled.choose_refresh(0,0,64,8);  
                yield();
            }
        }
            
        if(icon_flg&0x0008)
        {

            for(x=-48;x<17;x+=2)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(wea_x,x,wea_less_cloudy);
                if(son_moon ==1)
                    oled.BMP(son_moon_x,0,wea_son);
                else if(!son_moon)
                    oled.BMP(son_moon_x,0,wea_moon);
                oled.choose_refresh(0,0,64,8);  
                yield();
            }
        }
    }
    set_cpu_freq(LOW_FREQ);
}

void UI::show_icon_out()
{
    /*
    8少云 4多云 2月亮 1太阳
    8大雨 4中雨 2小雨 1毛毛雨
    8中雪 4小雪 2雷电 1暴雨
                2霾  1雾
    */
    short x;
    u8 son_moon = 2;
    set_cpu_freq(HIGH_FREQ);
    oled.xy_set(0,0,64,8);
    if(!icon_flg)
    {
        for(x=0;x<65;x+=4)
        {
            oled.choose_clr(0,0,64,8);
            oled.BMP(0,x,bmp_cat);
            oled.choose_refresh(0,0,64,8);
            yield();
        }
    }
    else
    {
        if(icon_flg&0x0001)
            son_moon = 1;
        if(icon_flg&0x0002)    
            son_moon = 0;
        if(icon_flg&0x0004)
        {
            for(x=16;x>-45;x-=4)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(wea_x,x,wea_cloudy);
                if(son_moon ==1)
                    oled.BMP(son_moon_x,0,wea_son);
                else if(!son_moon)
                    oled.BMP(son_moon_x,0,wea_moon);
                oled.choose_refresh(0,0,64,8);  
                yield();
            }
        }
            
        if(icon_flg&0x0008)
        {
            for(x=16;x>-49;x-=4)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(wea_x,x,wea_less_cloudy);
                if(son_moon ==1)
                    oled.BMP(son_moon_x,0,wea_son);
                else if(!son_moon)
                    oled.BMP(son_moon_x,0,wea_moon);
                oled.choose_refresh(0,0,64,8);  
                yield();
            }
        }


        if(icon_flg&0x0001)
        {
            for(x=0;x<65;x+=4)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(son_moon_x,x,wea_son);
                oled.choose_refresh(0,0,64,8);
                yield();
            }
        }
            
        if(icon_flg&0x0002)
        {
            for(x=0;x<65;x+=4)
            {
                oled.choose_clr(0,0,64,8);
                oled.BMP(0,x,wea_moon);
                oled.choose_refresh(0,0,64,8);
                yield();
            }
                
        }
    }
    set_cpu_freq(LOW_FREQ);
    
}

// void UI::data_refresh()
// {
//     u8 x = 64;
//     icon_set(hweather.icon);
//     oled.choose_clr(64,0,48,2);
//     oled.choose_clr(64,2,64,6);
//     oled.xy_set(64,0,128,8);
//     oled.chinese(64,0,hweather.wind,16,1,0);
    
//     x += oled.num(64,16,pcf.month,2,16,LEFT,1)*8;
//     oled.str(x,16,"-",16,1,0);
//     x += 8;
//     oled.num(x,16,pcf.day,2,16,LEFT,1);
//     show_week(111);

//     oled.num(64,50,hweather.temp,2,16,LEFT,1);
//     oled.chinese(80,50,"摄",16,1,0);

//     oled.num(120,50,hweather.humidity,3,16,RIGHT,1);
//     oled.str(120,50,"%",16,1,0);

//     oled.chinese(64,33,hweather.weather,16,1,0);
//     oled.choose_refresh(64,0,64,8);
//     oled.xy_set(0,0,64,8);
//     Rssi_refresh(112);
// }


void UI::data_refresh_move_in()
{
    s8 show_x;
    u8 x = 64;

    set_cpu_freq(HIGH_FREQ);
    icon_set(hweather.icon);

    oled.xy_set(64,0,64,8);
    rssi_flg = 0;
    

    for(show_x=64;show_x>=0;show_x--)
    {
        oled.choose_clr(64,0,64,2);
        Rssi_refresh(112+show_x);
        oled.xy_set(64,0,128,8);
        oled.chinese(64+show_x,0,hweather.wind,16,1,0);
        oled.choose_refresh(64,0,64,2);
        yield();
    }

    for(show_x=64;show_x>-1;show_x--)
    {
        oled.choose_clr(64,2,64,2);
        x = 64;
        x += oled.num(64+show_x,16,pcf.month,2,16,LEFT,1)*8;
        oled.str(x+show_x,16,"-",16,1,0);
        x += 8;
        oled.num(x+show_x,16,pcf.day,2,16,LEFT,1);
        show_week(111+show_x);
        oled.choose_refresh(64,2,64,2);
        yield();
    }

    if(hweather.weather!="")
    {
        for(show_x=64;show_x>-1;show_x--)
        {
            oled.choose_clr(64,4,64,3);
            oled.chinese(64+show_x,33,hweather.weather,16,1,0);
            oled.choose_refresh(64,4,64,3);
            yield();
        }
    }
    if(hweather.icon)
    {
        for(show_x=64;show_x>-1;show_x--)
        {
            oled.choose_clr(64,6,64,2);
            x = 64;
            x += oled.num(64,50,hweather.temp,2,16,LEFT,1)*8;
            oled.chinese(x+show_x,50,"摄",16,1,0);

            oled.num(120+show_x,50,hweather.humidity,3,16,RIGHT,1);
            oled.str(120+show_x,50,"%",16,1,0);

            oled.choose_refresh(64,6,64,2);
            yield();
        }

    }
    rssi_flg = 1;
    set_cpu_freq(LOW_FREQ);
}
void UI::data_refresh_move_out()
{
    rssi_flg = 0;
    set_cpu_freq(HIGH_FREQ);
    if(hweather.icon)
        oled.roll(64,6,64,2,2,RIGHT,32);
    if(hweather.weather!="")
        oled.roll(64,4,64,2,2,RIGHT,32);
    oled.roll(64,2,64,2,2,RIGHT,32);
    oled.roll(64,0,64,2,2,RIGHT,32); 
    set_cpu_freq(LOW_FREQ);
}

void UI::show_week(u8 x)
{
    switch (pcf.week)//x = 111
    {
        case 0:oled.chinese(x,16,"天",16,1,0);break;
        case 1:oled.chinese(x,16,"一",16,1,0);break;
        case 2:oled.chinese(x,16,"二",16,1,0);break;
        case 3:oled.chinese(x,16,"三",16,1,0);break;
        case 4:oled.chinese(x,16,"四",16,1,0);break;
        case 5:oled.chinese(x,16,"五",16,1,0);break;
        case 6:oled.chinese(x,16,"六",16,1,0);break;  
    }
}
void UI::Rssi_refresh(u8 dat)
{
    const char *p;
    oled.xy_set(0,0,128,8);
    oled.choose_clr(112,0,15,2);
    if(rssi_show_mode)
    {
        u8 i;
        oled.BMP(dat,2,battery_bmp,1);
        if(adc.battery_adc_buf >= 790)
            i = (adc.battery_adc_buf - 790) / 21;
        else
            i = 0;

        if(i > 10)
            i = 10;
        for(u8 x = 0;x < i;x++)
        {  
            if((dat + 12 - x) < 128)
                oled.line(dat + 12 - x,5,1,4,1);
        }
    }
    else
    {
        if(WiFi.status() == WL_CONNECTED)
        {
            short x = WiFi.RSSI();
            if(x>-80)
                p=bmp_wifi[3];
            else if(x<=-80&&x>-88)
                p=bmp_wifi[2];
            else if(x<=-88&&x>-93)
                p=bmp_wifi[1];
            else
                p=bmp_wifi[0];     
        }
        else
            p = bmp_wifi[4];
        oled.BMP(dat,2,15,11,p,1);
    }
    if(rssi_flg)
    { 
        oled.choose_refresh(112,0,15,2);
    }   
    oled.xy_set(0,0,64,8);
}

void UI::set_cpu_freq(u8 mode)
{
    if(mode == HIGH_FREQ)
    {
        system_update_cpu_freq(160);
        high_freq_flg =1;
    }
    else
    {
        system_update_cpu_freq(80);
        high_freq_flg =0;
    }

}



void UI::love_show()
{
    if(pcf.hour == 13&&pcf.min == 14&&love_flg)
    {
      love_flg = 0;
      show_love();
    }
    else if(pcf.hour == 17&&pcf.min == 20&&love_flg)
    {
      love_flg = 0;
      show_love();
    }
    if(pcf.hour == 13&&pcf.min == 0&&!love_flg)
    {
      love_flg = 1;
    }
    if(pcf.hour == 17&&pcf.min == 0&&!love_flg)
    {
      love_flg = 1;
    }

}


void UI::button(u8 mode)
{
    s8 y;
    if(mode == IN)
        y = -15;
    else
        y = 0;
        oled.xy_set(0,0,128,8);
    for(;;)
    {
        oled.choose_clr(0,0,15,2);
        oled.choose_clr(113,0,15,2);
        oled.BMP(0,y,button_exit);
        oled.BMP(113,y,button_next);
        oled.choose_refresh(0,0,15,2);
        oled.choose_refresh(113,0,15,2);
        if(mode == IN)
            y ++;
        else
            y --;
        if(y == 1|| y == -16)
        break;
        yield();
    }
}


void UI::show_switch(short x,short y,u8 persent){//0-9
    oled.BMP(x,y+3,switch_turn);
    oled.BMP(x+persent,y,circle_shi,1);
}

void UI::turn_switch(short x,short y,u8 mode){
    s8 i;
    if(mode == ON)
    {
        for(i = 0;i<10;i++)
        {
            oled.choose_clr(x,y/8,18,2);
            show_switch(x,y,i);
            oled.choose_refresh(x,y/8,18,2);
            delay(20);
        }      
    }
    else
    {
        for(i = 9;i>-1;i--)
        {
            oled.choose_clr(x,y/8,18,2);
            show_switch(x,y,i);
            oled.choose_refresh(x,y/8,18,2);
           delay(20);
        } 
    }

}

void UI::circle_move(u8 circle,u8 mode)//circle:0空心 1：实心 mode:方向
{
    s8 i = 0;
    u8 set_buf;
    if(menu_page == 2)
        set_buf = set_menu_num;
    else
        set_buf = set_dat_num;
    if(mode == DOWN)
    {
        if((set_buf + 1) >= set_menu_num_max&&!circle)
            return ;
    }
    else if(mode == UP)
    {
        if(!set_buf&&!circle)
            return ;
    }
    else 
        return;

    while(1)
    {
        if(mode == UP)
        {
            i--;
        }
        else
        {
            i++;
        }
        oled.choose_clr(17,0,9,8);
        if(circle)
        {
            oled.BMP(17,3+choose_menu_num*24+i,circle_shi);
            if(choose_menu_num <3)
                oled.BMP(17,3+set_buf*24,circle_kong);
        }
        else
        {
            oled.BMP(17,3+set_buf*24+i,circle_kong);
            if(choose_menu_num <3)
                oled.BMP(17,3+choose_menu_num*24,circle_shi);
        }
        oled.choose_refresh(17,0,9,8);
        if(i == -24||i == 24)
        {
            if(circle)
            {
                if(mode == UP)
                    choose_menu_num --;
                else
                    choose_menu_num ++;
            }
            else
            {
                if(mode == UP)
                {
                    if(menu_page == 2)
                        set_menu_num --;
                    else
                        set_dat_num --;
                }
                else
                {
                    if(menu_page == 2)
                        set_menu_num ++;
                    else
                        set_dat_num ++;
                }
            }
            return;
        }
        yield();
    }
    
}

void UI::factory()
{
    u8 x;
    ui.set_cpu_freq(HIGH_FREQ);
    for(x=0;x<129;x+=4)
    {
        oled.clr();
        oled.str((x-128),0,"NO",16,1,0);
        oled.str(102+(x-128),0,"YES",16,1,0);
        oled.chinese(x-128,32,"是否恢复出厂设置",16,1,0);
        oled.choose_refresh(0,0,128,2);
        oled.choose_refresh(0,4,128,2);
        yield();
    }
    while(1)
    {
      x=key.get_once();
      if(x==RIGHT)
      {
        eeprom.data_init();
        pcf.begin();
        pcf.init();
        oled.begin();
        break;
      }
      else if(x==LEFT)
      {
        break;
      }     
      delay(25);
    }
    oled.roll(0,0,128,8,8,RIGHT,16);
    ui.set_cpu_freq(LOW_FREQ);

}

void UI::led_color_ch(short x,u8 mode)
{
    switch(mode)
    {
        case 0:oled.display_parameters(x,0,16,1,0);oled.chinese("渐变色");oled.str("1");break;
        case 1:oled.display_parameters(x,0,16,1,0);oled.chinese("渐变色");oled.str("2");break;
        case 2:oled.display_parameters(x,0,16,1,0);oled.chinese("固定色");break;
    }
}

void UI::led_color_ch_move()
{
    s8 x;
    u8 tmp = led.color_mode_buf + 1;
    if(tmp == 3)
        tmp = 0;
    oled.xy_set(32,0,96,2);
    for(x = 32; x > - 33; x --)
    {
        oled.choose_clr(32,0,64,2);
        led_color_ch(x,led.color_mode_buf);
        led_color_ch(x + 64,tmp);
        oled.choose_refresh(32,0,64,2);
        yield();
    }
    led.color_mode_buf = tmp;
    eeprom.write(led_color_add,led.color_mode_buf);
    if(!led.color_choose_mode)
        led_color_set_flg = 1;
    oled.xy_set(0,0,128,8);
}

void UI::color_mix_circle(short x)
{
    oled.line(x,45,64,3,1);
    oled.BMP(x + 55*led.color_mix_buf*410/4096,42,circle_shi);

}

void UI::color_mix_circle_move(u8 mode)
{
    u8 tmp = 32 + 55*led.color_mix_buf*410/4096;
    u8 tmp1;
    if(mode == LEFT && led.color_mix_buf <= 0)
        return;
    if(mode == RIGHT && led.color_mix_buf >= 10)
        return;
    
    if(mode == RIGHT)
        led.color_mix_buf ++;
    else
        led.color_mix_buf --;

    tmp1 = 32 + 55*led.color_mix_buf*410/4096;

    for(;;)
    {
        if(mode == RIGHT)
        {
            tmp ++;
        }
        else
        {
            tmp --;
        }
        oled.choose_clr(32,5,96,2);
        oled.line(32,45,64,3,1);
        oled.BMP(tmp,42,circle_shi);
        oled.choose_refresh(32,5,96,2);

        if(tmp == tmp1)
            break;
        delay(8);
    }
    eeprom.write(led_color_mix_add,led.color_mix_buf);
    if(led.color_mode_buf == 2&&led.color_choose_mode == 0)
        led_color_set_flg = 1;

}


void UI::location_ap_get()
{
    for(;;)
    {
        set_cpu_freq(HIGH_FREQ);
        oled.roll(0,0,128,8,8,LEFT,16);
        if(setwifi.smart_conect(config_location))
        {
            if(wifima.conect_last())
            {
                if(strlen(&wifima.location[0]) > 2)
                {
                    String location_buf;
                    if(strlen(&wifima.location_up[0]) > 2)
                        location_buf = wifima.location + "&adm=" + wifima.location_up;
                    else
                        location_buf = wifima.location;
                    if(hweather.get_city(location_buf))
                    {
                        location_text = wifima.location;
                        location_up_text = wifima.location_up;
                        longitude = "";
                        latitude = "";
                        eeprom.write(long_start_add,0);
                        eeprom.write(lat_start_add,0);
                        eeprom.write_str(location_start_add,location_text);
                        eeprom.write_str(location_up_start_add,location_up_text);
                        wea_updata_flg = 1;
                        break;
                    }
                }
                else
                {
                    if(hweather.get_city(wifima.longitude,wifima.latitude))
                    {
                        longitude = wifima.longitude;
                        latitude = wifima.latitude;
                        location_text = "";
                        eeprom.write_str(long_start_add,longitude);
                        eeprom.write_str(lat_start_add,latitude);
                        eeprom.write(location_start_add,0);
                        eeprom.write(location_up_start_add,0);
                        wea_updata_flg = 1;
                        break;
                    }
                }      
            }
            else
                break;
        }
        else
            break;
    }
    page_switch(LEFT);
}

void UI::sleep_wake_hour_move()
{
    u8 buf;
    u8 i;
    if(set_dat_num)
    {
        buf = wake_hour +1;
        if(buf>10)
            buf = 4;
    }
    else
    {
        buf = sleep_hour +1;
        if(buf == 24)
            buf = 0;
        else if(buf == 2)
            buf = 20;
    }
    for(i=48;i>31;i--)
    {
        oled.choose_clr(32,3*set_dat_num,16,2);
        if(set_dat_num)
            oled.num(i,24,wake_hour,2,16,RIGHT,1);
        else
            oled.num(i,0,sleep_hour,2,16,RIGHT,1);
        oled.num(i+16,24*set_dat_num,buf,2,16,RIGHT,1);
        oled.choose_refresh(32,3*set_dat_num,16,2);
        delay(10);
    }
    if(set_dat_num)
    {
        wake_hour = buf;
        eeprom.write(wake_hour_add,wake_hour);
    }
    else
    {
        sleep_hour = buf;
        eeprom.write(sleep_hour_add,sleep_hour);
    }
}


void UI::show_future_weather()
{
    s8 y;
    s8 i;
    s8 x;

    if(menu_page == 0)
    {
        show_icon_out();
        data_refresh_move_out();
        set_cpu_freq(HIGH_FREQ);
        for(i=2;i>=0;i--)
        {    
            for(y = -16;y<(24*i+1);y+=4)
            {
                x = 114;
                if(i == 2)
                    oled.choose_clr(0,0,128,8);
                else if(i == 1)
                    oled.choose_clr(0,0,128,5);
                else
                    oled.choose_clr(0,0,128,2);
                oled.xy_set(0,0,128,8);
                oled.num(16,y,pcf.day + i,2,16,RIGHT,1);
                oled.write_char(16,y,':',16,1);
                oled.chinese(114,y,"摄",16,1,0);
                x -= (oled.num(x,y,hweather.tempMax[i],2,16,RIGHT,1)+1) * 8;
                oled.write_char(x,y,'-',16,1);
                oled.num(x,y,hweather.tempMin[i],2,16,RIGHT,1);
                oled.xy_set(0,0,72,8);
                oled.chinese(24,y,hweather.textDay[i],16,1,0);
                oled.refresh();

            }
        }
        menu_page = 10;
        oled.xy_set(0,0,128,8);
        future_weather_text_x[0] = 24;
        future_weather_text_x[1] = 24;
        future_weather_text_x[2] = 24;
    }
    else if(menu_page == 10)
    {
        set_cpu_freq(HIGH_FREQ);
        oled.roll(0,6,128,2,2,DOWN,8);
        oled.roll(0,3,128,5,2,DOWN,20);
        oled.roll(0,0,128,8,4,DOWN,16);
        data_refresh_move_in();
        show_icon_in();
        menu_page = 0;
    }
}

void UI::future_text_move()
{
    u8 buf;
    oled.xy_set(24,0,72,8);
    for(u8 i;i<3;i++)
    {
        buf = strlen(&hweather.textDay[i][0]);
        if(buf>10)
        {
            oled.choose_clr(24,24*i/8,48,2);
            oled.chinese(future_weather_text_x[i]--,24*i,hweather.textDay[i],16,1,0);
            if(future_weather_text_x[i] < (23 - (buf / 3 * 16)))
                future_weather_text_x[i] = 72;
            oled.choose_refresh(24,24*i/8,48,2);
        }  
    }
}